from utils.bootstrap import BootStrapForm
from django import forms
from web.models import *
from utils.encrypt import md5
from django.core.exceptions import ValidationError

class CustomerAddForm(BootStrapForm,forms.ModelForm):
    exclude_filed_list = ['level']
    confirm_password = forms.CharField(
        label="密码确认",
        required=True,
        widget=forms.PasswordInput(render_value=False)
    )
    class Meta:
        model = Customer
        fields = ['username','mobile','password','confirm_password','level']
        widgets = {
            'level': forms.RadioSelect(attrs={'class':'form-radio'}),
        }

    def clean_password(self):
        password = self.cleaned_data.get('password')
        return md5(password)

    def clean_confirm_password(self):
        password = self.cleaned_data.get('password')
        confirm_password = md5(self.cleaned_data.get('confirm_password'))
        if password != confirm_password:
            raise ValidationError("密码不一致")
        return confirm_password

class CustomerResetForm(BootStrapForm,forms.ModelForm):
    confirm_password = forms.CharField(
        label="密码确认",
        required=True,
        widget=forms.PasswordInput(render_value=False)
    )
    class Meta:
        model = Customer
        fields = ['password','confirm_password']
        widgets = {
            'password': forms.PasswordInput(render_value=False),
        }

    def clean_password(self):
        password = self.cleaned_data.get('password')
        return md5(password)

    def clean_confirm_password(self):
        password = self.cleaned_data.get('password')
        confirm_password = md5(self.cleaned_data.get('confirm_password'))
        if password != confirm_password:
            raise ValidationError("密码不一致")
        return confirm_password

class CustomerChargeForm(BootStrapForm,forms.ModelForm):
    charge_type = forms.TypedChoiceField(
        label="类型",
        choices=[(1, "充值"), (2, "扣款")],  # 只适合固定的数据，不适合去数据表中数据
        coerce=int
    )
    class Meta:
        model = TransactionRecord
        fields = ['charge_type','amount']

